package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.OrderService;
import com.qigao.mall.commons.dto.OrderExcelDto;
import com.qigao.mall.commons.dto.OrderModiDto;
import com.qigao.mall.commons.dto.OrderQueryDto;
import com.qigao.mall.commons.dto.OrderResultDto;
import com.qigao.mall.commons.enums.OrderActionEnum;
import com.qigao.mall.commons.enums.OrderStatusEnum;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.IDUtils;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractOrderController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.vo.app.OrderConfirmSendVo;
import com.qigao.mall.web.vo.app.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/admin/order/")
@Api(tags = "Admin商户操作订单相关API" ,value = "Admin商户操作订单相关API")
public class AdminOrderController extends AbstractOrderController {

    private static Logger logger= LoggerFactory.getLogger(AdminOrderController.class);
    @Resource
    private OrderService orderService;

    @PostMapping("/sendGoods")
    @ApiOperation(value = "确认发货", notes = "确认发货")
    public Result<String> sendGoods(@RequestBody OrderConfirmSendVo orderConfirmSendVo, HttpServletRequest request){
        //参数校验
        try{
            OrderModiDto orderModiDto=new OrderModiDto();
            orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
            orderModiDto.setOrderAction(OrderActionEnum.CONFIRM_SEND);
            orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
            orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            orderModiDto.setOrderStatus(OrderStatusEnum.HAS_SEND.getCode());
            orderModiDto.setShippingWay(orderConfirmSendVo.getShippingWay());
            orderModiDto.setShippingNo(orderConfirmSendVo.getShippingNo());
            return orderService.updateOrderStatus(orderModiDto);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @PostMapping("/confirmMoneyBatch")
    @ApiOperation(value = "批量确认收钱", notes = "批量确认收钱")
    public Result<String> confirmMoneyBatch(@RequestBody List<OrderConfirmSendVo> orderConfirmSendVos, HttpServletRequest request){
        //参数校验
        try{
            if(CollectionUtils.isEmpty(orderConfirmSendVos)){
                return Result.newError(ResultCode.COMMON_PARAM_NULL);
            }
            List<OrderModiDto> orderModiDtos=new ArrayList<>();
            for(OrderConfirmSendVo orderConfirmSendVo:orderConfirmSendVos){
                OrderModiDto orderModiDto=new OrderModiDto();
                orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
                orderModiDto.setOrderAction(OrderActionEnum.CONFIRM_MONEY);
                orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
                orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
                orderModiDto.setOrderStatus(OrderStatusEnum.HAS_PAY.getCode());
                orderModiDtos.add(orderModiDto);
            }
            return orderService.updateOrderStatusBatch(orderModiDtos);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @PostMapping("/cancelBatch")
    @ApiOperation(value = "批量作废", notes = "批量作废")
    public Result<String> cancelBatch(@RequestBody List<OrderConfirmSendVo> orderConfirmSendVos, HttpServletRequest request){
        //参数校验
        try{
            if(CollectionUtils.isEmpty(orderConfirmSendVos)){
                return Result.newError(ResultCode.COMMON_PARAM_NULL);
            }
            List<OrderModiDto> orderModiDtos=new ArrayList<>();
            for(OrderConfirmSendVo orderConfirmSendVo:orderConfirmSendVos){
                OrderModiDto orderModiDto=new OrderModiDto();
                orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
                orderModiDto.setOrderAction(OrderActionEnum.ORDER_CANCEL);
                orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
                orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
                orderModiDto.setOrderStatus(OrderStatusEnum.CLOSE.getCode());
                orderModiDtos.add(orderModiDto);
            }
            return orderService.updateOrderStatusBatch(orderModiDtos);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @PostMapping("/delBatch")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result<String> delBatch(@RequestBody List<OrderConfirmSendVo> orderConfirmSendVos, HttpServletRequest request){
        //参数校验
        try{
            if(CollectionUtils.isEmpty(orderConfirmSendVos)){
                return Result.newError(ResultCode.COMMON_PARAM_NULL);
            }
            List<OrderModiDto> orderModiDtos=new ArrayList<>();
            for(OrderConfirmSendVo orderConfirmSendVo:orderConfirmSendVos){
                OrderModiDto orderModiDto=new OrderModiDto();
                orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
                orderModiDto.setOrderAction(OrderActionEnum.ORDER_DELETE);
                orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
                orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
                orderModiDto.setOrderStatus(OrderStatusEnum.DELETED.getCode());
                orderModiDtos.add(orderModiDto);
            }
            return orderService.updateOrderStatusBatch(orderModiDtos);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //2.按照订单状态查询订单列表
    @PostMapping("/listPage")
    @ApiOperation(value = "按照订单状态查询所有用户订单列表", notes = "按照订单状态查询所有用户订单列表")
    public Result<PageInfo<OrderResultDto>> listPage(@RequestBody OrderQueryVo orderQueryVo, HttpServletRequest request){
        try{
            //参数校验
            OrderQueryDto orderQueryDto=new OrderQueryDto();
            BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
            if(StringUtils.isNotEmpty(orderQueryVo.getOrderStatus()))
            orderQueryDto.setOrderStatus(Integer.valueOf(orderQueryVo.getOrderStatus()));
            if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateBegin())){
                orderQueryDto.setOrderDateBegin(orderQueryVo.getOrderDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateEnd())){
                orderQueryDto.setOrderDateEnd(orderQueryVo.getOrderDateEnd()+" 23:59:59");
            }
            if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateBegin())){
                orderQueryDto.setConfirmDateBegin(orderQueryVo.getConfirmDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateEnd())){
                orderQueryDto.setConfirmDateEnd(orderQueryVo.getConfirmDateEnd()+" 23:59:59");
            }
            return  orderService.findOrderByCond(orderQueryDto);
        }catch (Exception e){
            logger.error("[按照订单状态查询所有用户订单列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(OrderQueryVo orderQueryVo,HttpServletResponse response) {
        OrderQueryDto orderQueryDto=new OrderQueryDto();
        BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderStatus()))
            orderQueryDto.setOrderStatus(Integer.valueOf(orderQueryVo.getOrderStatus()));
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateBegin())){
            orderQueryDto.setOrderDateBegin(orderQueryVo.getOrderDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateEnd())){
            orderQueryDto.setOrderDateEnd(orderQueryVo.getOrderDateEnd()+" 23:59:59");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateBegin())){
            orderQueryDto.setConfirmDateBegin(orderQueryVo.getConfirmDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateEnd())){
            orderQueryDto.setConfirmDateEnd(orderQueryVo.getConfirmDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<OrderResultDto> orderResultDtos = orderService.findOrderResultByCond(orderQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(getDataList(orderResultDtos), getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "orderReport", excelUtils.getWorkbook());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody OrderQueryVo orderQueryVo,HttpServletResponse response) {
        OrderQueryDto orderQueryDto=new OrderQueryDto();
        BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderStatus()))
            orderQueryDto.setOrderStatus(Integer.valueOf(orderQueryVo.getOrderStatus()));
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateBegin())){
            orderQueryDto.setOrderDateBegin(orderQueryVo.getOrderDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getOrderDateEnd())){
            orderQueryDto.setOrderDateEnd(orderQueryVo.getOrderDateEnd()+" 23:59:59");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateBegin())){
            orderQueryDto.setConfirmDateBegin(orderQueryVo.getConfirmDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(orderQueryVo.getConfirmDateEnd())){
            orderQueryDto.setConfirmDateEnd(orderQueryVo.getConfirmDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<OrderResultDto> orderResultDtos = orderService.findOrderResultByCond(orderQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(getDataList(orderResultDtos), getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "orderReport", excelUtils.getWorkbook());
    }

    private List getDataList(List<OrderResultDto> orderResultDtos) {
        List<OrderExcelDto> orderExcelDtos=new ArrayList<>();
        for(OrderResultDto orderResultDto:orderResultDtos){
            OrderExcelDto orderExcelDto=new OrderExcelDto();
            BeanUtils.copyProperties(orderResultDto,orderExcelDto);
            orderExcelDto.setRecvPhone(orderResultDto.getOrderShippingResultDto().getRecvPhone());
            orderExcelDto.setOrderNum(orderResultDto.getOrderDetailResultDtoList().get(0).getNum());
            orderExcelDto.setCustomerName(orderResultDto.getOrderShippingResultDto().getRecvName());
            orderExcelDtos.add(orderExcelDto);
        }
        return orderExcelDtos;
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "订单号"),
                new ExcelHeaderInfo(0, 0, 1, 1, "订单时间"),
                new ExcelHeaderInfo(0, 0, 2, 2, "客户姓名"),
                new ExcelHeaderInfo(0, 0, 3, 3, "客户手机号"),
                new ExcelHeaderInfo(0, 0, 4, 4, "客户获得积分"),
                new ExcelHeaderInfo(0, 0, 5, 5,  "销售数量"),
                new ExcelHeaderInfo(0, 0, 6, 6, "销售金额"),
                new ExcelHeaderInfo(0, 0, 7, 7, "优惠金额"),
                new ExcelHeaderInfo(0, 0, 8, 8, "积分抵扣金额"),
                new ExcelHeaderInfo(0, 0, 9, 9,  "实际收款金额"),
                new ExcelHeaderInfo(0, 0, 10,10, "销售人员"),
                new ExcelHeaderInfo(0, 0, 11,11, "所属公司"),
                new ExcelHeaderInfo(0, 0, 12,12, "订单状态"),
                new ExcelHeaderInfo(0, 0, 13,13, "购买方式")

        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }

}
